
//
//  LSHomelessBeggerInfoDetailViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/1/26.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSHomelessBeggerInfoDetailViewController.h"
#import "LSHomelessBeggerInfoTableViewCell.h"

@interface LSHomelessBeggerInfoDetailViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray <NSDictionary *> *contentArray;
@property (nonatomic, strong) NSArray <NSDictionary *> *recsArray;
@property (nonatomic, strong) NSArray <NSDictionary *> *recsContentArray;
@end

static NSString *LSHomelessBeggerInfoTableViewCellID = @"LSHomelessBeggerInfoTableViewCell";

@implementation LSHomelessBeggerInfoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"详情";
    [self.view addSubview:self.tableView];
    [self loadData];
}

- (void)loadData {
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:VagrancyDetail param:@{@"id":_dataFromWebDict[@"id"],@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token]} completionHandler:^(id responseObject, NSError *connectionError) {
        NSNumber *status = [responseObject objectForKey:@"status"];
        if (status.integerValue == 200) {
            NSArray *dataArray = responseObject[@"data"];
            NSDictionary *dataDict = dataArray.firstObject;
            self.dataFromWebDict = dataDict[@"homeless"];
            self.recsArray = dataDict[@"recs"];
            [self.tableView reloadData];
        } else {
            [MBProgressHUD showTipMessageInWindow:[ValidateUtils validateNull:responseObject[@"message"]]];
        }
    }];
}

- (NSString *)timeStampToDate:(NSString *)timeStampString {
    // iOS 生成的时间戳是10位
    if (![[ValidateUtils validateNull:timeStampString] isEqualToString:@""] && !([ValidateUtils validateNull:timeStampString].integerValue == 0)) {
        NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
        NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate: date];
    } else {
        return @"";
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.recsArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.contentArray.count;
    } else {
        return self.recsContentArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSHomelessBeggerInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSHomelessBeggerInfoTableViewCellID forIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSDictionary *dataDict = self.contentArray[indexPath.row];
        cell.mainTitleLabel.text = dataDict[@"title"];
        [cell.mainTitleLabel sizeToFit];
        if ([self.dataFromWebDict[dataDict[@"content"]] isKindOfClass:[NSNumber class]]) {
            if ([dataDict[@"content"] isEqualToString:@"birthday"]) {
                NSString *stirng = self.dataFromWebDict[@"birthday"];
                cell.subTitleLabel.text = [self timeStampToDate:stirng];
            } else if ([dataDict[@"content"] isEqualToString:@"mls"]) {
                NSNumber *stirng = self.dataFromWebDict[@"mls"];
                if (stirng.integerValue == 2) {
                    cell.subTitleLabel.text = @"农村五保";
                } else if (stirng.integerValue == 1) {
                    cell.subTitleLabel.text = @"低保";
                } else {
                    cell.subTitleLabel.text = @"暂无";
                }
            } else {
                NSNumber *stirng = self.dataFromWebDict[dataDict[@"content"]];
                cell.subTitleLabel.text = stirng.stringValue;
            }
            cell.subTitleTextView.text = @"";
        } else {
            if (indexPath.row > 5 && indexPath.row < 11) {
                cell.subTitleLabel.text = @"";
                cell.subTitleTextView.text = [ValidateUtils validateNull:self.dataFromWebDict[dataDict[@"content"]]];
            } else {
                cell.subTitleLabel.text = [ValidateUtils validateNull:self.dataFromWebDict[dataDict[@"content"]]];
                cell.subTitleTextView.text = @"";
            }
        }
        return cell;
    } else {
        NSDictionary *dataDict = self.recsContentArray[indexPath.row];
        cell.mainTitleLabel.text = dataDict[@"title"];
        [cell.mainTitleLabel sizeToFit];
        if ([self.recsArray[indexPath.section-1][dataDict[@"content"]] isKindOfClass:[NSNumber class]]) {
            if ([dataDict[@"content"] isEqualToString:@"appealDate"]) {
                NSString *stirng = self.recsArray[indexPath.section-1][@"appealDate"];
                cell.subTitleLabel.text = [self timeStampToDate:stirng];
            } else if ([dataDict[@"content"] isEqualToString:@"civilSignDate"]) {
                NSString *stirng = self.recsArray[indexPath.section-1][@"civilSignDate"];
                cell.subTitleLabel.text = [self timeStampToDate:stirng];
            } else if ([dataDict[@"content"] isEqualToString:@"streetSignDate"]) {
                NSString *stirng = self.recsArray[indexPath.section-1][@"streetSignDate"];
                cell.subTitleLabel.text = [self timeStampToDate:stirng];
            } else {
                NSNumber *stirng = self.recsArray[indexPath.section-1][dataDict[@"content"]];
                cell.subTitleLabel.text = stirng.stringValue;
            }
            cell.subTitleTextView.text = @"";
        } else {
            if (indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 6 || indexPath.row == 8 || indexPath.row == 9) {
                cell.subTitleLabel.text = @"";
                cell.subTitleTextView.text = [ValidateUtils validateNull:self.recsArray[indexPath.section-1][dataDict[@"content"]]];
            } else {
                cell.subTitleLabel.text = [ValidateUtils validateNull:self.recsArray[indexPath.section-1][dataDict[@"content"]]];
                cell.subTitleTextView.text = @"";
            }
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row > 5 && indexPath.row < 11) {
            NSDictionary *dataDict = self.contentArray[indexPath.row];
            CGRect TitleRect = [[ValidateUtils validateNull:self.dataFromWebDict[dataDict[@"content"]]] boundingRectWithSize:CGSizeMake(ScreenWidth-24, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14]} context:nil];
            return TitleRect.size.height+44+30;
        }
        return LSHomelessBeggerInfoTableViewCellHeight;
    } else {
        if (indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 6 || indexPath.row == 8 || indexPath.row == 9) {
            NSDictionary *dataDict = self.recsContentArray[indexPath.row];
            CGRect TitleRect = [[ValidateUtils validateNull:self.recsArray[indexPath.section-1][dataDict[@"content"]]] boundingRectWithSize:CGSizeMake(ScreenWidth-24, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14]} context:nil];
            return TitleRect.size.height+44+30;
        }
        return LSHomelessBeggerInfoTableViewCellHeight;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    sectionHeaderView.backgroundColor = UIColor.whiteColor;
    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, ScreenWidth-24, 40) andText:[NSString stringWithFormat:@"救助记录%ld",(long)section] andTextColor:UIColor.blackColor andFont:[UIFont fontWithName:@"SourceHanSansCN-Bold" size:15] andTextAlignment:NSTextAlignmentCenter];
    [sectionHeaderView addSubview:sectionHeaderLabel];
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0f;
    } else {
        return 40;
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:LSHomelessBeggerInfoTableViewCell.class forCellReuseIdentifier:LSHomelessBeggerInfoTableViewCellID];
    }
    return _tableView;
}

- (NSArray<NSDictionary *> *)contentArray {
    if (!_contentArray) {
        _contentArray = @[
                          @{@"title":@"姓名：",@"content":@"name"},
                          @{@"title":@"性别：",@"content":@"genderName"},
                          @{@"title":@"本人成分：",@"content":@"compenentname"},
                          @{@"title":@"文化程度：",@"content":@"educationname"},
                          @{@"title":@"身份证：",@"content":@"idcard"},
                          @{@"title":@"出生日期：",@"content":@"birthday"},
                          @{@"title":@"身体特征：",@"content":@"feature"},
                          @{@"title":@"申请救助事项及理由：",@"content":@"helpItems"},
                          @{@"title":@"户籍地址：",@"content":@"registeredResidence"},
                          @{@"title":@"住址：",@"content":@"address"},
                          @{@"title":@"亲属(姓名、关系、电话)：",@"content":@"kinship"},
                          @{@"title":@"是否享受低保或农村五保：",@"content":@"mls"}//0暂无 1低保 2农村五保
                          ];
    }
    return _contentArray;
}

- (NSArray<NSDictionary *> *)recsArray {
    if (!_recsArray) {
        _recsArray = @[];
    }
    return _recsArray;
}

- (NSArray<NSDictionary *> *)recsContentArray {
    if (!_recsContentArray) {
        _recsContentArray = @[
                          @{@"title":@"救助类型：",@"content":@"helpTypeName"},
                          @{@"title":@"救助时间：",@"content":@"appealDate"},
                          @{@"title":@"申请救助对象及理由：",@"content":@"helpItems"},
                          @{@"title":@"救助人：",@"content":@"helpSign"},
                          @{@"title":@"流浪经历(原因、时间、经过)：",@"content":@"wanderExp"},
                          @{@"title":@"随身携带信息(名称、数量、估计)：",@"content":@"carryAbout"},
                          @{@"title":@"沟通结果：",@"content":@"commuResult"},
                          @{@"title":@"联系人：",@"content":@"linkman"},
                          @{@"title":@"街道民政意见：",@"content":@"civilAdvice"},
                          @{@"title":@"街道意见：",@"content":@"streetAdvice"},
                          @{@"title":@"街道民政签字人：",@"content":@"civilSignatory"},
                          @{@"title":@"街道签字人：",@"content":@"streetSignatory"},
                          @{@"title":@"街道民政签字日期：",@"content":@"civilSignDate"},
                          @{@"title":@"街道签字日期：",@"content":@"streetSignDate"}
                          ];
    }
    return _recsContentArray;
}

- (void)setDataFromWebDict:(NSDictionary *)dataFromWebDict {
    _dataFromWebDict = dataFromWebDict;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
